package com.azusa.ktc_qa.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import utils.IdWorker;
import com.azusa.ktc_qa.dao.ReplyDao;
import com.azusa.ktc_qa.entity.Reply;

/**
 * @Description 回答 服务层
 * @author Azusa
 * @date 2020-10-12 18:29:45
*/
@Service
public class ReplyServiceImpl implements ReplyService{

	@Autowired
	private ReplyDao replyDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Reply> findAll() {
		return replyDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Reply> findSearch(Map whereMap, int page, int size) {
		Specification<Reply> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return replyDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Reply> findSearch(Map whereMap) {
		Specification<Reply> specification = createSpecification(whereMap);
		return replyDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Reply findById(String id) {
		return replyDao.findById(id).get();
	}

	/**
	* 增加
	* @param reply
	*/
	public void add(Reply reply) {
		reply.setId( idWorker.nextId()+"" );
		replyDao.save(reply);
	}

	/**
	* 修改
	* @param reply
	*/
	public void update(Reply reply) {
		replyDao.save(reply);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		replyDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Reply> createSpecification(Map searchMap) {

		return new Specification<Reply>() {

			@Override
			public Predicate toPredicate(Root<Reply> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//问题ID
				if (searchMap.get("problemid")!=null && !"".equals(searchMap.get("problemid"))) {
					predicateList.add(cb.like(root.get("problemid").as(String.class), "%"+(String)searchMap.get("problemid")+"%"));
				}
				//回答内容
				if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
					predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
				}
				//创建日期
				if (searchMap.get("createtime")!=null && !"".equals(searchMap.get("createtime"))) {
					predicateList.add(cb.like(root.get("createtime").as(String.class), "%"+(String)searchMap.get("createtime")+"%"));
				}
				//更新日期
				if (searchMap.get("updatetime")!=null && !"".equals(searchMap.get("updatetime"))) {
					predicateList.add(cb.like(root.get("updatetime").as(String.class), "%"+(String)searchMap.get("updatetime")+"%"));
				}
				//回答人ID
				if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
					predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
				}
				//回答人昵称
				if (searchMap.get("nickname")!=null && !"".equals(searchMap.get("nickname"))) {
					predicateList.add(cb.like(root.get("nickname").as(String.class), "%"+(String)searchMap.get("nickname")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
